<?php
/**************************************************
*  Created:  2010-10-28
*
*  网站自定义配置项管理类
*
*  @Xweibo (C)1996-2099 SINA Inc.
*  @Author liwen <liwen2@staff.sina.com.cn>
*
***************************************************/
class sysConfig_mod  extends modules_base {

	///初始值 
	private $_options = array(   
							'ad_setting' 				=> '',					//运营位相关设置
							'hotel_official_account'	=> '2011787914'			//酒店首页官方微博帐号
						);	

	/**
	 * 保存系统配置信息
	 * @return bool|null
	 */
	function set($key, $value)
	{
		if (isset($this->_options[$key])) {
			$db = APP::ADP('db');
			$sql = $db->SQL("REPLACE `%s` SET `key` = '%s',`value` = '%s'", $db->getTable(T_SYS_CONFIG), $key, $value);
	        if ($db->execute($sql) !== false) {
				$this->get($key, true);
				return true;
	        }
		}
		
        return false;
	}

	/**
	 * 获取网站自定义配置信息
	 *
	 * @param string|array $key 如:rewrite_enable
	 * $key为空时取全部配置信息
	 * @return array|null
	 */
	function get($keys = null, $reset = false)
	{
		if (empty($keys))	$keys = array_keys($this->_options);
		if (!is_array($keys))	$keys = array($keys);
		
		return $this->mutiCacheCall('get_db', array($keys), array('kname' => 'sysconfig', 'kvars' => $keys), $reset);
	}
	
	/**
	 * 获取网站自定义配置信息
	 *
	 * @param string|array $key 如:rewrite_enable
	 * $key为空时取全部配置信息
	 * @return array|null
	 */
	function get_db($keys = null)
	{
		$rs = null;
		$db = APP :: ADP('db');
		
		if (empty($keys))	$keys = array_keys($this->_options);
		if (!is_array($keys))	$keys = array($keys);
		
		$sql = 'SELECT * FROM ' . $db->getTable(T_SYS_CONFIG);
		$sql .= " WHERE `key` IN (" . $db->buildIn($keys) . ")";
		
		if ($result = $db->query($sql)) {
			$rs = $this->_options;
			foreach($result as $row) {
				if (!isset($rs[$row['key']]))	continue;
	            $rs[$row['key']] = $row['value'];
	        }
		}
        
		return $rs;
	}
}